<template>
  <div id="lux-aside"
       class="fl">
    <el-popover ref="popoverCountry"
                @show="popoverShow('country')"
                @hide="popoverHide"
                placement="right-start"
                width="300"
                trigger="click">
      <el-input prefix-icon="el-icon-search"
                @input="searchChange('country')"
                v-model="search.country"></el-input>
      <div class="wrap countrywrap">
        <ul>
          <li v-for="option in options">
            <el-checkbox :label="option.name"
                         :class="{'active': option.value}"
                         v-model="option.value"
                         @change="checkboxChange('country', option)"></el-checkbox>
          </li>
        </ul>
      </div>
    </el-popover>
    <el-popover ref="popoverRetailer"
                @show="popoverShow('retailer')"
                @hide="popoverHide"
                placement="right"
                width="300"
                trigger="click">
      <div class="search">
        <el-input prefix-icon="el-icon-search"
                  @input="searchChange('retailer')"
                  v-model="search.retailer"></el-input>
      </div>
      <div class="wrap retailerwrap">
        <ul>
          <li v-for="option in options">
            <el-checkbox :label="option.name"
                         :class="{'active': option.value}"
                         v-model="option.value"
                         @change="checkboxChange('retailer', option)"></el-checkbox>
          </li>
        </ul>
      </div>
    </el-popover>
    <el-popover ref="popoverBrands"
                @show="popoverShow('brands')"
                @hide="popoverHide"
                placement="right"
                width="300"
                trigger="click">
      <div class="search">
        <el-input prefix-icon="el-icon-search"
                  @input="searchChange('brands')"
                  v-model="search.brands"></el-input>
      </div>
      <div class="wrap brandswrap">
        <el-collapse accordion>
          <el-collapse-item v-for="option in options"
                            :key="'collapse'+ option.name">
            <template slot="title">
              <el-checkbox :label="option.name"
                           v-model="option.value"
                           :class="{'active': option.value}"
                           @change="checkboxChange('brands', option, $event)"></el-checkbox>
            </template>
            <ul>
              <li v-for="item in option.models">
                <el-checkbox :label="item.name"
                             :class="{'active': item.value}"
                             @change="checkboxChange('collection', item,$event)"
                             v-model="item.value"></el-checkbox>
              </li>
            </ul>
          </el-collapse-item>
        </el-collapse>
      </div>
    </el-popover>

    <el-popover ref="popoverMaterial"
                @show="popoverShow('material')"
                @hide="popoverHide"
                placement="right"
                width="300"
                trigger="click">
      <div class="search">
        <el-input prefix-icon="el-icon-search"
                  @input="searchChange('material')"
                  v-model="search.material"></el-input>
      </div>
      <div class="wrap materialwrap">
        <ul>
          <li v-for="option in options">
            <el-checkbox :label="option.name"
                         v-model="option.value"
                         :class="{'active': option.value}"
                         @change="checkboxChange('material', option)"></el-checkbox>
          </li>
        </ul>
      </div>
    </el-popover>
    <el-popover ref="popoverColor"
                @show="popoverShow('color')"
                @hide="popoverHide"
                placement="right"
                width="280"
                trigger="click">
      <div class="wrap colorwrap">
        <el-col :span="12"
                v-for="color in colors"
                :class="{active: color.active}"
                :key="color.value">
          <div @click="colorChange(color)">

            <span v-if="color.value"
                  class="color"
                  :style="{background: color.value}"></span>
            <span v-else-if="color.id === 13"
                  class="color"
                  style="background: linear-gradient(90deg, #f8ea9b, #e0c00f);"></span>
            <span v-else-if="color.id === 14"
                  class="color"
                  style="background: linear-gradient(90deg, #e65148, #ebcd18, #1885eb, #18eb1d);"></span>
            <span v-else-if="color.id === 21"
                  class="color"
                  style="background: linear-gradient(90deg, #dee0e3, #9ca1aa );"></span>
            <span>{{color.name}}</span>
          </div>

        </el-col>
      </div>
    </el-popover>
    <el-popover ref="popoverClassification"
                @show="popoverShow('type')"
                @hide="popoverHide"
                placement="right"
                width="300"
                trigger="click">
      <div class="search">
        <el-input prefix-icon="el-icon-search"
                  @input="searchChange('type')"
                  v-model="search.type"></el-input>
      </div>
      <div class="wrap">
        <ul>
          <li v-for="option in options">
            <el-checkbox :label="option.name"
                         v-model="option.value"
                         :class="{'active': option.value}"
                         @change="checkboxChange('type', option)"></el-checkbox>
          </li>
        </ul>
      </div>
    </el-popover>
    <el-popover ref="popoverCondition"
                @show="popoverShow('condition')"
                @hide="popoverHide"
                placement="right"
                width="300"
                trigger="click">
      <div class="wrap">
        <ul>
          <li v-for="option in options">
            <el-checkbox :label="option.name"
                         v-model="option.value"
                         :class="{'active': option.value}"
                         @change="checkboxChange('condition', option)"></el-checkbox>
          </li>
        </ul>
      </div>
    </el-popover>
    <div class="filter">
      FILTER
    </div>
    <ul class="out">
      <li>
        <div class="title"
             v-popover:popoverCountry>
          COUNTRY
          <div class="fr">
            <img src="../assets/icons/Country.png"
                 alt="">
          </div>
        </div>
        <ul class="inner">
          <li v-for="item in selectCountry"
              :key="'country' + item.location_id">
            <span>{{item.name}}</span>
            <span class="fr"
                  @click="remove('country',item.location_id)">x</span>
          </li>
        </ul>
      </li>
      <li>
        <div class="title"
             v-popover:popoverRetailer>
          RETAILER
          <div class="fr">
            <img src="../assets/icons/Retailer.png"
                 alt="">
          </div>
        </div>
        <ul class="inner">
          <li v-for="item in selectRetailer"
              :key="'retailer' + item.id">
            <span>{{item.name}}</span>
            <span class="fr"
                  @click="remove('retailer',item.id)">x</span>
          </li>
        </ul>
      </li>
      <li>
        <div class="title"
             v-popover:popoverBrands>
          BRANDS
          <div class="fr">
            <img src="../assets/icons/Brands.png"
                 alt="">
          </div>
        </div>
        <ul class="inner">
          <li v-for="item in selectBrands"
              :key="'brands'  + item.id">
            <span>{{item.name}}</span>
            <span class="fr"
                  @click="remove('brands',item.id)">x</span>
          </li>
        </ul>
      </li>
      <li>
        <div class="title"
             v-popover:popoverMaterial>
          MATERIAL
          <div class="fr">
            <img src="../assets/icons/Material.png"
                 alt="">
          </div>
        </div>
        <ul class="inner">
          <li v-for="item in selectMaterial"
              :key="'material'+ item.id">
            <span>{{item.name}}</span>
            <span class="fr"
                  @click="remove('material',item.id)">x</span>
          </li>
        </ul>
      </li>
      <li>
        <div class="title"
             v-popover:popoverColor>
          COLOR
          <div class="fr">
            <img src="../assets/icons/Colour.png"
                 alt="">
          </div>
        </div>
        <ul class="inner">
          <li v-for="item in selectColor"
              :key=" 'color' +item.id">
            <span>{{item.name}}</span>
            <span class="fr"
                  @click="remove('color',item.id)">x</span>
          </li>
        </ul>
      </li>
      <li>
        <div class="title"
             v-popover:popoverClassification>
          CATEGORY
          <div class="fr">
            <img src="../assets/icons/Classification.png"
                 alt="">
          </div>
        </div>
        <ul class="inner">
          <li v-for="item in selectType"
              :key=" 'type' +item.id">
            <span>{{item.name}}</span>
            <span class="fr"
                  @click="remove('type',item.id)">x</span>
          </li>
        </ul>
      </li>
      <li>
        <div class="title"
             v-popover:popoverCondition>
          CONDITIONS
          <div class="fr">
            <img src="../assets/icons/condition.png"
                 alt="">
          </div>
        </div>
        <ul class="inner">
          <li v-for="item in selectCondition"
              :key=" 'type' +item.id">
            <span>{{item.name}}</span>
            <span class="fr"
                  @click="remove('condition',item.id)">x</span>
          </li>
        </ul>
      </li>
    </ul>
  </div>
</template>

<script>
import { colors } from "@/util/config";
import { mapGetters } from "vuex";

const conditionArr = [
  {
    id: 1,
    name: "New (one-hundred)",
    describe: "New In",
    value: false
  },
  {
    id: 2,
    name: "Excellent (ninety-five)",
    describe: "Excellent. Almost as good as new.",
    value: false
  },
  {
    id: 3,
    name: "Good (ninety)",
    describe:
      "Good. There are a few scratches, stains and looks a little used.",
    value: false
  },
  {
    id: 4,
    name: "Fair (eighty)",
    describe:
      "Fair. There are some noticeable scratches and stains. looks obvious used.",
    value: false
  },
  {
    id: 5,
    name: "Poor (seventy)",
    describe: "Poor. Looks very used, but still usable.",
    value: false
  }
];

export default {
  data() {
    return {
      options: [],
      activeName: 1,
      search: {},
      timer: "",
      colors: colors,
      init: 0,
      modelPopover: false,
      modelX: 500,
      modelY: 100,
      brandModels: []
    };
  },
  computed: {
    collectionActive() {
      return !!this.selectBrands.length;
    }
  },
  created() {
    this.getData("retailer");
    this.getData("brands");
  },
  methods: {
    add(type, option) {
      switch (type) {
        case "country":
          this.$store.commit("addCountry", option);
          break;
        case "retailer":
          this.$store.commit("addRetailer", option);
          break;
        case "brands":
          this.$store.commit("addBrands", option);
          break;
        case "material":
          this.$store.commit("addMaterial", option);
          break;
        case "color":
          this.$store.commit("addColor", option);
          break;
        case "type":
          this.$store.commit("addClassification", option);
          break;
        case "condition":
          this.$store.commit("addCondition", option);
          break;
        case "collection":
          this.$store.commit("addCollection", option);
          break;
      }
    },
    remove(type, id) {
      switch (type) {
        case "country":
          this.$store.commit("removeCountry", id);
          break;
        case "retailer":
          this.$store.commit("removeRetailer", id);
          break;
        case "brands":
          this.$store.commit("removeBrands", id);
          break;
        case "material":
          this.$store.commit("removeMaterial", id);
          break;
        case "color":
          this.$store.commit("removeColor", id);
          break;
        case "type":
          this.$store.commit("removeClassification", id);
          break;
        case "condition":
          this.$store.commit("removeCondition", id);
          break;
        case "collection":
          this.$store.commit("removeCollection", id);
          break;
      }
    },
    checkboxChange(type, option, event = "") {
      switch (type) {
        case "country":
          option.value
            ? this.add(type, option)
            : this.remove(type, option.location_id);
          break;
        case "retailer":
          option.value ? this.add(type, option) : this.remove(type, option.id);
          break;
        case "brands":
          this.brandsCheckChange(type, option);
          break;
        case "material":
          option.value ? this.add(type, option) : this.remove(type, option.id);
          break;
        case "color":
          option.value ? this.add(type, option) : this.remove(type, option.id);
          break;
        case "type":
          option.value ? this.add(type, option) : this.remove(type, option.id);
          break;
        case "condition":
          option.value ? this.add(type, option) : this.remove(type, option.id);
          break;
        case "collection":
          option.value ? this.add(type, option) : this.remove(type, option.id);
          break;
      }
    },
    brandsCheckChange(type, option) {
      let models = option.models;
      if (option.value) {
        models.forEach(item => (item.value = true));
        this.add(type, option);
      } else {
        models.forEach(item => (item.value = false));
        this.remove(type, option.id);
      }
    },
    colorChange(color) {
      color.active = !color.active;
      color.active ? this.add("color", color) : this.remove("color", color.id);
    },
    searchChange(type) {
      clearTimeout(this.timer);
      switch (type) {
        case "country":
          this.timer = setTimeout(() => {
            this.getData(type, "country");
          }, 500);
          break;
        case "retailer":
          this.timer = setTimeout(() => {
            this.getData(type, "retailer");
          }, 500);
          break;
        case "brands":
          this.timer = setTimeout(() => {
            this.getData(type, "brands");
          }, 500);
          break;
        case "material":
          this.timer = setTimeout(() => {
            this.getData(type, "material");
          }, 500);
          break;
        case "color":
          this.timer = setTimeout(() => {
            this.getData(type, "color");
          }, 500);
          break;
        case "type":
          this.timer = setTimeout(() => {
            this.getData(type, "type");
          }, 500);
          break;
      }
    },
    popoverHide() {
      this.search = {};
      this.options = [];
    },
    popoverShow(type) {
      if (type === "condition") {
        this.options = JSON.parse(JSON.stringify(conditionArr));
        for (let i of this.selectCondition) {
          for (let j of this.options) {
            if (i.id === j.id) {
              j.value = true;
            }
          }
        }
      } else if (type === "color") {
        this.colors.forEach(item => (item.active = false));
        for (let i of this.selectColor) {
          for (let j of this.colors) {
            if (i.id === j.id) {
              j.active = true;
            }
          }
        }
      } else {
        this.getData(type);
      }
    },
    getData(type, params) {
      const urlParams = {
        field: this.search[params],
        filter: this.filter
      };
      this.$http.post(type, urlParams).then(res => {
        res.forEach(item => {
          item.value = false;
          if (item.models) {
            let models = item.models;
            models.forEach(item => (item.value = false));
          }
        });
        this.options = res;
        if (this.init < 2) {
          if (type === "retailer") {
            this.$store.commit("initRetailer", res.slice(0, 2));
          } else if (type === "brands") {
            let hermes = res.find(item => item.id === 1);
            let lv = res.find(item => item.id === 9);
            let dior = res.find(item => item.id === 3);
            this.$store.commit("initBrands", [hermes, lv, dior]);
          }
          this.init++;
        }
        if (type === "country") {
          for (let i of this.selectCountry) {
            for (let j of this.options) {
              if (i.location_id === j.location_id) {
                j.value = true;
              }
            }
          }
        } else if (type === "material") {
          this.options.forEach(item => (item.name = item.name.split("/")[0])); // 数据过滤  去掉/后面部分
          for (let i of this["select" + firstWordUp(type)]) {
            for (let j of this.options) {
              if (i.id === j.id) {
                j.value = true;
              }
            }
          }
        } else if (type === "brands") {
          let selectCollectionId = this.selectCollection.map(item => item.id);
          for (let i of this.selectBrands) {
            for (let j of this.options) {
              if (i.id === j.id) {
                j.value = true;
              }
            }
          }
          for (let j of this.options) {
            for (let z of j.models) {
              if (selectCollectionId.includes(z.id)) {
                z.value = true;
              }
            }
          }
        } else {
          for (let i of this["select" + firstWordUp(type)]) {
            for (let j of this.options) {
              if (i.id === j.id) {
                j.value = true;
              }
            }
          }
        }
      });
      function firstWordUp(str) {
        return str[0].toUpperCase() + str.substring(1);
      }
    }
  }
};
</script>

<style lang="scss">
@import "../assets/styles/color.scss";

#lux-aside {
  .model-popover {
    width: 200px;
    height: 400px;
    overflow: auto;
    background-color: gray;
    position: absolute;
    z-index: 3;
    text-align: left;
    text-indent: 3px;
  }
  transition: 1.5s;
  width: 260px;
  height: 100%;
  background-color: $sideColor;
  overflow: hidden;
  ul {
    list-style-type: none;
    padding: 0px;
    margin: 0px;
    li {
      border-bottom: 2px solid $leftOutBorder;
      box-shadow: 0 1px rgba(255, 255, 255, 0.4);
      box-sizing: border-box;
    }
    &.out > li {
      cursor: pointer;
      font-size: 0.9rem;
      text-align: left;
      padding-left: 8px;
      padding-right: 5px;
      .title {
        height: 2.4rem;
        line-height: 2.4rem;
        color: #333;
        img {
          width: 1.15rem;
          height: 1.15rem;
          position: relative;
          right: 6px;
          top: 6px;
        }
      }
      .inner {
        li {
          color: #666;
          background-color: $sideInputColor;
          text-indent: 8px;
          height: 1.8rem;
          line-height: 1.8rem;
          font-size: 0.8rem;
          border-bottom: none;
          margin-bottom: 4px;
          span {
            display: inline-block;
            max-width: 200px;
            text-overflow: ellipsis;
            overflow: hidden;
          }
          .fr {
            color: #666;
            margin-right: 5px;
            &:hover {
              color: red;
            }
          }
        }
      }
    }
  }
  li {
    list-style: none;
  }
  .filter {
    font-size: 1rem;
    text-align: left;
    background-color: #313132;
    padding-left: 8px;
    padding-right: 5px;
    height: 2.2rem;
    line-height: 2.2rem;
    color: rgba(255, 255, 255, 0.5);
  }
}
.el-popover {
  background: $sideColor !important;
  border: none !important;
  border-radius: 2px;
  padding: 5px;
  line-height: 2rem;
  .wrap {
    &.brandswrap {
      .brand-li {
        position: relative;
        .fold {
          z-index: 10;
          position: absolute;
          top: 0;
          right: 1px;
          color: #666;
          display: block;
          width: 20px;
          height: 20px;
          cursor: pointer;
          &:hover {
            color: $base;
          }
        }
      }
    }
    & > ul {
      margin-right: 5px;
    }
    max-height: 400px;
    overflow-y: auto;
    overflow-x: hidden;
    &::-webkit-scrollbar {
      width: 8px;
      background-color: $sideColor;
    }
    &::-webkit-scrollbar-thumb {
      background-color: #f4f9fe;
      border-radius: 0;
      height: 30px;
      width: 1px;
    }
    &::-webkit-scrollbar-track {
      background-color: rgba(255, 255, 255, 0.3);
      border-radius: 0;
      width: 50px;
    }
    &::-webkit-scrollbar-track-piece {
      background-color: rgba(255, 255, 255, 0.3);
      border-radius: 0;
      width: 50px;
    }

    &.colorwrap {
      color: #666;
      .el-col {
        padding: 5px;
        cursor: pointer;
        div {
          padding-left: 5px;
          border-radius: 2px;
        }
        &.active {
          color: $base;
          div {
            background-color: $sideInputColor;
          }
        }
      }
      .color {
        display: inline-block;
        height: 12px;
        width: 12px;
        border-radius: 2px;
        margin-right: 10px;
      }
    }
  }
  .el-input {
    padding: 0;
    margin-top: 6px;
    margin-bottom: 8px;
  }
  .el-input__icon {
    color: $base;
    line-height: 30px;
    font-size: 18px;
    left: 0;
    width: 20px;
  }
  .el-input__inner {
    height: 1.7rem;
    border-radius: 2px;
  }
  .el-checkbox.active {
    .el-checkbox__input {
      .el-checkbox__inner {
        border-color: $base;
      }
    }
  }
  .el-checkbox__input {
    .el-checkbox__inner {
      border-color: #333;
      background-color: transparent !important;
      &:after {
        border-color: $base;
      }
    }
  }
  ul {
    list-style-type: none;
    padding: 0px;
    margin: 0px;
  }
  &.brandswrap {
    ul {
      margin-right: 0;
    }
  }
  li {
    &:nth-child(odd) {
      background-color: #f7f7f8;
    }
    &:nth-child(even) {
      background-color: #ecebee;
    }
    text-indent: 3px;
    list-style: none;
    height: 1.9rem;
    line-height: 1.9rem;
    background-color: $sideInputColor;
    overflow: hidden;
    .el-checkbox {
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
      display: block;
      color: #4c4c4c;
      font-size: 0.7rem;
      .el-checkbox__inner {
        background-color: transparent;
        border-color: #333;
        margin-bottom: 2px;
        &::after {
          color: $base;
        }
      }
      .el-checkbox__label {
      }
    }
  }
}
.popper__arrow::after {
  display: none !important;
}
.el-collapse {
  border-top: none !important;
  border-bottom: none !important;
  margin-right: 5px;
}

.el-collapse-item {
  &:nth-child(odd) {
    .el-collapse-item__header {
      background-color: #f7f7f8 !important;
    }
    li {
      background-color: #f7f7f8;
    }
  }
  &:nth-child(even) {
    .el-collapse-item__header {
      background-color: #ecebee !important;
    }
    li {
      background-color: #ecebee !important;
    }
  }
}
.el-collapse-item__wrap {
  border-bottom: none !important;
}

.el-collapse-item__header {
  color: #fff !important;
  border-bottom: none;
  text-indent: 5px;
  height: 1.9rem !important;
  line-height: 1.9rem !important;
}
.el-collapse-item__content {
  padding-bottom: 0 !important;
  .el-checkbox__input {
    text-indent: 30px;
  }
}

.el-icon-arrow-right {
  color: #666;
}
.el-collapse-item__arrow {
  line-height: 1.9rem !important;
}
</style>
